package aplicacao.movel.coleta;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import br.usp.fsi.entity.Local;
import br.usp.fsi.entity.Registro;

import android.content.Context;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextView;
import aplicacao.movel.R;
import aplicacao.movel.webservice.Tipo;
import aplicacao.movel.webservice.WebServiceREST;

public class ColetaAdapter extends BaseAdapter {
	private List<Registro> coletas;
	private Context context;
	private LayoutInflater inflater;
	private TextView local;
	private TextView dataCriacao;
	private TextView dataProposta;
	private TextView dataColeta;
	private TextView comentario;
	private Local localColeta;

	public ColetaAdapter(Context context, int textViewResourceId, List<Registro> objects) {
		this.context = context;
		this.coletas = objects;
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	private LayoutInflater mInflater;
	//private ArrayList<Registro> itens = new ArrayList <Registro>();


	public void AdapterListView(Context context, ArrayList<Registro> itens) {
		//Itens que preencheram o listview
		this.coletas = itens;
		//responsavel por pegar o Layout do item.
		mInflater = LayoutInflater.from(context);
	}


	public int getCount() {
		return coletas.size();
	}

	public Object getItem(int posicao) {
		return coletas.get(posicao);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View view, ViewGroup parent) {
		
		Registro col = coletas.get(position);
		
		if (view == null){
			view = inflater.inflate(R.layout.detalhescoletarow, null);
		}
		
		local =  (TextView) view.findViewById(R.id.txtLocal);
		dataCriacao = (TextView) view.findViewById(R.id.txtDataCriacao);
		dataProposta = (TextView) view.findViewById(R.id.txtDataProposta);
		dataColeta = (TextView) view.findViewById(R.id.txtDataColeta);
		comentario = (TextView) view.findViewById(R.id.txtComentarios);

		try {
			Local localColeta = new Local ();
			localColeta = (Local) new WebServiceREST().obterUm(Tipo.Local, col.getIdLocal());
			local.setText (localColeta.getNome());
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		Calendar data;
		data = Calendar.getInstance();
		
		data.setTimeInMillis(col.getDataCriacao());
		dataCriacao.setText("Criacao "+DateFormat.format("dd/MM/yy", data));
		data.setTimeInMillis(col.getDataProposta());
		dataProposta.setText("Proposta "+DateFormat.format("dd/MM/yy", data));
		data.setTimeInMillis(col.getDataColeta());
		dataColeta.setText("Coleta "+DateFormat.format("dd/MM/yy", data));
		comentario.setText(col.getComentario());
		
		return view;
	}

	
}
